<script setup lang="ts">
import type { VxeGridProps } from '#/adapter/vxe-table';

import { Page, useVbenModal } from '@vben/common-ui';

import { Space } from 'ant-design-vue';

import { useVbenVxeGrid } from '#/adapter/vxe-table';
import {
  changeMessageReadStatus,
  // deleteAllMessage,
  markMessageAllRead,
  queryUserMessagePage,
} from '#/api/notification';

import previewModal from './components/preview-modal.vue';
import { columns } from './data';

const gridOptions: VxeGridProps = {
  checkboxConfig: {
    // 高亮
    highlight: true,
    // 翻页时保留选中状态
    reserve: true,
    // 点击行选中
    // trigger: 'row',
  },
  // 需要使用i18n注意这里要改成getter形式 否则切换语言不会刷新
  // columns: columns(),
  columns,
  height: 'auto',
  keepSource: true,
  pagerConfig: {},
  proxyConfig: {
    ajax: {
      query: async ({ page }, formValues = {}) => {
        return await queryUserMessagePage({
          pageNum: page.currentPage,
          pageSize: page.pageSize,
          ...formValues,
        });
      },
    },
  },
  rowConfig: {
    keyField: 'id',
  },
  id: 'notification-list-index',
};

const [BasicTable, tableApi] = useVbenVxeGrid({
  gridOptions,
});

// 预览
const [PreviewModal, previewModalApi] = useVbenModal({
  connectedComponent: previewModal,
});

function handlePreview(row: any) {
  previewModalApi.setData({ title: row.title, content: row.content });
  previewModalApi.open();
}

async function markRead(row: any) {
  await changeMessageReadStatus({ id: row.id, readStatus: 1 });
  await tableApi.query();
}

async function markAllRead() {
  await markMessageAllRead();
  await tableApi.query();
}

// async function handleDeleteAll() {}
</script>

<template>
  <Page :auto-content-height="true">
    <BasicTable>
      <template #table-title>
        <main-title text="消息列表" />
      </template>
      <template #toolbar-tools>
        <Space>
          <a-button @click="markAllRead">全部标为已读</a-button>
        </Space>
        <!--<a-button danger type="primary" @click="handleDeleteAll">-->
        <!--  清空全部消息-->
        <!--</a-button>-->
      </template>
      <template #content="{ row }">
        <div v-html="row.content"></div>
      </template>
      <template #readStatus="{ row }">
        <span v-if="row.readStatus === 0">未读</span>
        <span v-if="row.readStatus === 1" class="text-[#C0C4CC]">已读</span>
      </template>
      <template #action="{ row }">
        <Space>
          <ghost-button @click.stop="handlePreview(row)"> 查看</ghost-button>
          <ghost-button v-if="row.readStatus === 0" @click.stop="markRead(row)">
            标为已读
          </ghost-button>
        </Space>
      </template>
    </BasicTable>
    <PreviewModal />
  </Page>
</template>
